package eventloop;

import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import user.Publish;
import user.PublishGroup;
import user.Receive;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageEventLoopGroup extends DefaultEventLoopGroup {

    //推流客户端
    private Map<String, Publish> publishMap = new HashMap<String, Publish>();


    //拉流端
    private Map<String, List<Receive>> receiveMap = new HashMap<String, List<Receive>>();


    /**
     * 设置 推流 channel
     * @param path
     * @param client
     */
    public synchronized void setPublishChannel(String path, Publish client) {
        publishMap.put(path,client);
    }

    /**
     * 获取 推流 channel
     * @param path
     * @return
     */
    public synchronized Publish getPublishChannel(String path) {
        if(publishMap.containsKey(path)){
            return publishMap.get(path);
        }
        return null;
    }


    /**
     * 设置 拉流 channel
     * @param path
     * @param client
     */
    public synchronized  void setReceiveChannel(String path,Receive client) {
        List<Receive> list = new ArrayList<Receive>();
        if(receiveMap.containsKey(path)){
            list = receiveMap.get(path);
            list.add(client);
        } else {
            list = new ArrayList<Receive>();
            list.add(client);
        }
        receiveMap.put(path,list);
    }

    /**
     * 设置 拉流 channel
     * @param path
     */
    public synchronized void setReceiveChannel(String path,List<Receive> list) {
        receiveMap.put(path,list);
    }

    /**
     * 获取 拉流 channel
     * @param path
     * @return
     */
    public  List<Receive> getReceiveChannel(String path) {
        if(receiveMap.containsKey(path)){
            return receiveMap.get(path);
        }
        return null;
    }

    private int time = 0;
    public void setVideoMessage(String path, byte[] data, int timestamp){
        List<Receive> list = this.receiveMap.get(path);
        Publish publish = this.publishMap.get(path);
        this.execute(new VideoRunnable(data, publish, list, timestamp));
    }

    public void setAudioMessage(String path, byte[] data, int timestamp){
        List<Receive> list = this.receiveMap.get(path);
        this.execute(new AudioRunnable(data, list, timestamp));
    }

}
